﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Common;
using Model;

namespace BusStationClient
{
    class DrawBusLineImage
    {


        private const int startPosX = 130;    //左边起始点 X坐标
        private const int stationCount = 10;  //每条线路显示公交个数

        //public string CurrentStation = "工商局";
        //public string NextStation = "耀邦红军小学";


        public string CurrentStation = "烟草公司路口";
        public string NextStation = "普法长廊";

        Dictionary<string, string[]> StationLinesList = new Dictionary<string, string[]>();


        public Bitmap DrwaImage(int width, int height, ArrayList arrayList = null)
        {

            //MainForm.BusList.CopyTo(array,0);

            try
            {
                #region


                GetAllStationLines();
                int sumWidth = width - 150; //一个公交线路总宽度
                Bitmap bMap = new Bitmap(width, height);
                Graphics g = Graphics.FromImage(bMap);
                g.Clear(Color.White);

                int strartPosY = height / (StationLinesList.Count + 1);
                //  _stations1.
                int lineWidth = (sumWidth - 120) / (stationCount - 1);  //根据公交个数来算出线条宽度

                g.DrawString("当前站点为： " + CurrentStation + "        下一站：" + NextStation, new Font("微软雅黑", 14), new SolidBrush(Color.Red), 35, 10);

                int i = 0;
                foreach (KeyValuePair<string, string[]> keyValuePair in StationLinesList)
                {
                    i++;
                    string fileNameLine = "pk" + (i % 4 + 1).ToString() + ".png";
                    string fileNamePoint = "p" + (i % 4 + 1).ToString() + ".png";
                    Bitmap tempPointKImg = new Bitmap(fileNameLine);
                    Bitmap tempPointImg = new Bitmap(fileNamePoint);


                    Color c = tempPointImg.GetPixel(tempPointImg.Width / 2, tempPointImg.Height / 2);
                    float penHeight = 2.2f;
                    Pen pen = new Pen(c, penHeight);

                    string[] tmpStationLins = GetTheLineStations(keyValuePair.Value);

                    for (int j = 0; j < tmpStationLins.Count(); j++)
                    {

                        int nStrartPosX = startPosX + (lineWidth) * j;
                        int nStrartPosY = strartPosY * i;
                        if (tmpStationLins[j] == CurrentStation)
                        {
                            //画当前站点  实心 圆点
                            g.DrawImage(tempPointImg, nStrartPosX, nStrartPosY, tempPointImg.Width, tempPointImg.Height);
                            g.DrawString(tmpStationLins[j], new Font("微软雅黑", 8), new SolidBrush(Color.Red), nStrartPosX - 20, nStrartPosY + 15);
                        }
                        else
                        {
                            //画当前站点 空心  圆点
                            g.DrawImage(tempPointKImg, nStrartPosX, nStrartPosY, tempPointKImg.Width, tempPointKImg.Height);
                            g.DrawString(tmpStationLins[j], new Font("微软雅黑", 8), new SolidBrush(Color.Black), nStrartPosX - 20, nStrartPosY + 15);
                        }

                        if (j != tmpStationLins.Count() - 1)
                        {
                            // 画线
                            g.DrawLine(pen, nStrartPosX + tempPointImg.Width
                                , nStrartPosY + (tempPointImg.Height - penHeight) / 2,
                                nStrartPosX + lineWidth,
                                nStrartPosY + (tempPointImg.Height - penHeight) / 2);
                        }


                        if (arrayList != null)
                        {
                            //Console.WriteLine("dictionary.Count  : " + arrayList.Count);

                            Bitmap tempBus = Properties.Resources._29;

                            foreach (var obj in arrayList)
                            {
                                BusInfo b = obj as BusInfo;
                                if (b != null && b.BusName.Trim() == keyValuePair.Key && b.CurrentStation.Trim() == tmpStationLins[j])
                                {
                                    int index = GetIndexofStation(keyValuePair.Value, b.CurrentStation);
                                    if (index == 0 || b.PreStation.Trim() == keyValuePair.Value[index - 1])
                                    {
                                        g.DrawImage(tempBus, nStrartPosX - 20, nStrartPosY-15, tempBus.Width, tempBus.Height);
                                    }
                                }
                            }





                        }



                    }
                    ////画公交线路名称
                    Rectangle lbRect = new Rectangle(startPosX - 90, strartPosY * i - 8, 100, 20);
                    //StringFormat drawFormat = new StringFormat();
                    //drawFormat.Alignment = StringAlignment.Far;
                    //// drawFormat.LineAlignment = StringAlignment.Far;
                    g.DrawString(keyValuePair.Key, new Font("微软雅黑", 12), new SolidBrush(Color.Black), lbRect);
                }

                return bMap;

                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                //MessageBox.Show(ex.Message);
            }
            return new Bitmap(width, height);

            //string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
            //pictureBox1.Image.Save(fileName);
        }


        //根据站点名称返回改站点有几条线路。
        private void GetAllStationLines()
        {
            try
            {
                #region
                StationLinesList.Clear();
                string stationName = "," + CurrentStation + ",";

                string tempLine1 = "," + StationInfo.Line1 + ",";
                string tempLine3 = "," + StationInfo.Line3 + ",";
                string tempLine2 = "," + StationInfo.Line2 + ",";

                string[] stationLists1 = StationInfo.Line1.Split(',');
                string[] stationLists3 = StationInfo.Line3.Split(',');
                string[] stationLists2 = StationInfo.Line2.Split(',');

                if (tempLine1.Contains(stationName))
                {
                    StationLinesList.Add(StationInfo.stationgNamesArray[0], stationLists1);
                }
                if (tempLine2.Contains(stationName))
                {
                    StationLinesList.Add(StationInfo.stationgNamesArray[1], stationLists2);
                }

                if (tempLine3.Contains(stationName))
                {
                    StationLinesList.Add(StationInfo.stationgNamesArray[2], stationLists3);
                }

                bool mustRevse = false;
                foreach (KeyValuePair<string, string[]> keyValuePair in StationLinesList)
                {
                    string[] tmp = keyValuePair.Value;
                    int index = GetIndexofStation(tmp);
                    if (index != tmp.Count() - 1 && index != 0 && (tmp[index + 1] != NextStation && tmp[index - 1] == NextStation))
                    {
                        mustRevse = true;
                        break;
                    }
                    else if (index == tmp.Count() - 1 && tmp[index - 1] == NextStation)
                    {
                        mustRevse = true;
                        break;
                    }
                    else if (index == 0 && ("" == NextStation || (NextStation == tmp[tmp.Count() - 2] && tmp[0] == tmp[tmp.Count() - 1])))
                    {
                        mustRevse = true;
                        break;
                    }
                }

                if ((CurrentStation == "烟草公司路口" && NextStation == "普法长廊")
                    || (CurrentStation == "普法长廊" && NextStation == "共青中心城")
                    || (CurrentStation == "深傲公司" && NextStation == "烟草公司路口")
                    || (CurrentStation == "回圆公司" && NextStation == "深傲公司")
                    || (CurrentStation == "天湖丽都" && NextStation == "回圆公司"))
                {
                    mustRevse = true;
                }



                if (mustRevse)
                {


                    Dictionary<string, string[]> tmp = new Dictionary<string, string[]>();
                    foreach (KeyValuePair<string, string[]> keyValuePair in StationLinesList)
                    {
                        List<string> tmpStations=new List<string>();
                        for (int i = 0; i < keyValuePair.Value.Count(); i++)
                        {
                            if (keyValuePair.Value[i] == "汽车站")
                            {
                                keyValuePair.Value[i] = "深傲公司";
                            }
                            if (keyValuePair.Value[i] == "邦华苑小区")
                            {
                                keyValuePair.Value[i] = "回圆公司";
                            }
                            if (keyValuePair.Value[i] == "中国银行")
                            {
                                keyValuePair.Value[i] = "普法长廊";
                            }
                            if (keyValuePair.Value[i] != "财政局")
                            {
                                tmpStations.Add(keyValuePair.Value[i]);
                            }
                        }


                        tmp.Add(keyValuePair.Key, tmpStations.ToArray().Reverse().ToArray());
                    }
                    StationLinesList = tmp;
                }
                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }


        }

        /// <summary>
        /// 获取当前站点 在当前线路 的索引
        /// </summary>
        /// <param name="strings"></param>
        /// <returns></returns>
        private int GetIndexofStation(string[] strings)
        {
            int index = 0;
            for (int i = 0; i < strings.Count(); i++)
            {
                if (CurrentStation.Equals(strings[i]))
                {
                    index = i;
                    break;
                }
            }
            return index;
        }

        private int GetIndexofStation(string[] strings, string stationName)
        {
            int index = 0;
            for (int i = 0; i < strings.Count(); i++)
            {
                if (stationName.Equals(strings[i]))
                {
                    index = i;
                    break;
                }
            }
            return index;
        }



        /// <summary>
        /// 根据当前站点名称，来获取改站点的前面8个站点和后面一个站点（单条线路站点数小于10会有问题，暂不考虑）
        /// </summary>
        /// <param name="lineStations"></param>
        /// <returns></returns>
        private string[] GetTheLineStations(string[] lineStations)
        {
            List<string> tmpStrArray = new List<string>();
            try
            {
                #region
                int tmpIndex = 8;//索引8,站点索引小于8，则直接画出该条线路的前十个站点。
                if (lineStations[0] != lineStations[lineStations.Count() - 1])
                {
                    int index = GetIndexofStation(lineStations);
                    if (index < tmpIndex)
                    {
                        for (int i = 0; i < 10; i++)
                        {
                            tmpStrArray.Add(lineStations[i]);
                            // tmpStrArray[i] = lineStations[i];
                        }
                    }
                    else if (index >= tmpIndex && index < lineStations.Count() - 1)
                    {
                        for (int i = 0; i < 10; i++)
                        {
                            tmpStrArray.Add(lineStations[i + index - tmpIndex]);
                        }
                    }
                    else if (index == lineStations.Count() - 1)
                    {
                        for (int i = 0; i < 10; i++)
                        {
                            tmpStrArray.Add(lineStations[i + index - tmpIndex - 1]);
                        }
                    }
                }
                else  //循环线路
                {
                    int index = GetIndexofStation(lineStations);
                    if (index < tmpIndex)
                    {
                        int count = tmpIndex - index;

                        for (int i = lineStations.Count() - count - 1; i < lineStations.Count() - 1; i++)
                        {
                            tmpStrArray.Add(lineStations[i]);
                        }
                        for (int i = 0; i <= index + 1; i++)
                        {
                            tmpStrArray.Add(lineStations[i]);
                        }

                    }
                    else if (index >= tmpIndex && index < lineStations.Count() - 1)
                    {
                        for (int i = 0; i < 10; i++)
                        {
                            tmpStrArray.Add(lineStations[i + index - tmpIndex]);
                        }
                    }

                }

                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return tmpStrArray.ToArray();
        }
    }
}
